package com.atguigu.dao.impl;

import com.atguigu.dao.DeliveryDao;
import com.atguigu.pojo.Delivery;
import com.atguigu.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DeliveryDaoImpl implements DeliveryDao {
    QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public List<Delivery> findAllDeliveryByUserId(Integer userId) throws SQLException {
        String sql = "select id id,delivery_name deliveryName,user_id userId,company_id companyId,phone phone,address address," +
                "send_time sendTime,state state from sys_delivery where user_id = ?";
        List<Delivery> query = queryRunner.query(sql, new BeanListHandler<>(Delivery.class), userId);
        return query;
    }

    /**
     * 添加快递
     * @param uid
     * @param parameterMap
     */
    @Override
    public void addDelivery(Integer uid, Map<String, String[]> parameterMap) throws SQLException {
        String sql = "insert into sys_delivery values (null,?,?,?,?,?,?,?)";
        queryRunner.update(sql,parameterMap.get("deliveryName")[0],uid,parameterMap.get("companyId")[0],
                parameterMap.get("phone")[0],parameterMap.get("address")[0],parameterMap.get("sendTime")[0],parameterMap.get("state")[0]);
    }
}
